import { excelJsonExport } from '@vimi/utils-tool'
import { mapGetters } from 'vuex'

const enumStatus = {
  0: '历史项目',
  1: '未申请审查',
  2: '待业务部负责人审核',
  3: '待风险经理审核',
  4: '待风险部负责人审核',
  5: '待评审委员会审核',
  6: '待评审会主任审核',
  // 并行
  10: '待业务部负责人、风险经理审核',
  11: '待业务部负责人、风险部负责人审核',
  200: '已通过',
  300: '已拒绝',
}
const enumBusRisk = {
  '0-0-0': 10,
  '0-1-0': 11,
  '0-1-1': 2,
  '1-0-0': 3,
  '1-1-0': 4,
}

const getEditable = (status, { job, isApprovalRep, isProcessPointRisk }) => {
  switch (status) {
    case 1:
      return 0
    case 2:
      return job === 'org_c_manage' ? 1 : 0
    case 3:
      return isProcessPointRisk === 1 ? 2 : 0
    case 4:
      return job === 'org_r_manage' ? 3 : 0
    case 5:
      return isApprovalRep === 1 ? 4 : 0
    case 6:
      return job === 'org_corp' ? 5 : 0
    case 10:
      return job === 'org_c_manage' ? 1 : isProcessPointRisk === 1 ? 2 : 0
    case 11:
      return job === 'org_c_manage' ? 1 : job === 'org_r_manage' ? 3 : 0
    case 200:
      return 0
    case 300:
      return 0
    default:
      return 0
  }
}

export const statusAdapter = (item, user) => {
  const batchApproval = item.review_approval_batch

  let batch = {}
  if (batchApproval && batchApproval.length) {
    batch = batchApproval[0]
  } else {
    ;[
      'project_status',
      'res_status_point',
      'res_status_corp',
      'res_status_risk',
      'res_status_risk_point',
      'review_approval',
      'res_status_bus',
    ].forEach(key => {
      batch[key] = item[key]
    })
  }

  let status = 1
  if (+item.is_history === 1) {
    status = 0
  } else if (+item.project_status === 3 || +batch.res_status_corp === 2) {
    status = 300
  } else if (+batch.res_status_point === 0) {
    status = 1
  } else if (+batch.res_status_corp === 1) {
    status = 200
  } else if (+batch.review_approval === 2) {
    status = 6
  } else if (+batch.res_status_risk === 1 && +batch.res_status_bus === 1) {
    status = 5
  } else {
    status =
      enumBusRisk[`${batch.res_status_bus}-${batch.res_status_risk_point}-${batch.res_status_risk}`]
  }

  Object.keys(batch).forEach(key => {
    item[key] = batch[key]
  })

  item.status = status
  item.statusLabel = enumStatus[status]
  item.editable = getEditable(status, user)
}

export default {
  data() {
    return {
      loading: false,
      tableData: [],
      tableCount: 0,
      exportLoading: false,
    }
  },
  computed: {
    ...mapGetters(['user', 'userId', 'clientId', 'job', 'org']),
  },
  methods: {
    getParams() {
      return {
        ...this.params,
        orWhereAndTemplate: 1,
        'orWhereAnd.is_ex_approval.eq': 0,
        'orWhereAnd.import_status.eq': 1,

        'orWhereAnd.is_ex_approval.in': 1,
        'orWhereAnd.is_pass.eq': 1,

        'orWhere.service_id.eq': this.clientId,
        'orWhere.recommend_service.eq': this.clientId,
      }
    },
    fetchData(params) {
      return this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, params, this.order || {})
      )
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res = {}] = await this.fetchData(this.getParams())
      this.loading = false

      const data = res.data || {}
      this.tableCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.dataAdapter)
    },
    async exportExcel(filename) {
      const exportTable = this.tableColumns.filter(item => !item.operations)
      const header = {}
      const headerCellStyles = {}
      const columns = exportTable.map((item, i) => {
        const { prop, label } = item.attrs || {}
        header[prop] = label
        headerCellStyles[`${String.fromCharCode(65 + i)}1`] = { font: { bold: true } }
        return prop
      })

      this.exportLoading = true
      const { index, size, ...params } = this.getParams()
      const [res = {}] = await this.fetchData(params)
      this.exportLoading = false

      const data = (res.data || []).map(this.dataAdapter)

      excelJsonExport({
        columns,
        data: [header, ...data],
        filename,
        styles: {
          border: {
            left: { style: 'thin' },
            top: { style: 'thin' },
            right: { style: 'thin' },
            bottom: { style: 'thin' },
          },
          alignment: {
            vertical: 'center',
            horizontal: 'center',
            wrapText: true,
          },
        },
        cellStyles: {
          ...headerCellStyles,
        },
      })
    },
    statusAdapter(item) {
      return statusAdapter(item, this.user)
    },
  },
}
